package info.techgeek.labs.timer;

import info.techgeek.labs.connection.DAOConnectionManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Timer;
import java.util.TimerTask;

public class QueryTimer {

	private final Timer timer = new Timer();
	private final int minutes;
	private Connection conn = null;

	public QueryTimer(int minutes) {

		this.minutes = minutes;

	}

	public void start() {
		System.out.println("Begin Start");

		PreparedStatement pstmt = null;
		ResultSet rs = null;
		long t = System.currentTimeMillis(); 

		String qryString = getQuery(); 
		try{

			conn= DAOConnectionManager.getInstance().getConnection(null);
			System.out.println("conn "+conn);
			System.out.println(qryString);
			pstmt = conn.prepareStatement(qryString);

			timer.schedule(new TimerTask() {
				public void run() {
					System.out.println("Run ");
					closeConnection();
					timer.cancel();
				}
				private void closeConnection() {
					try {
						System.out.println("Close Connection "+conn);
						conn.close();
						System.out.println("Close Connection "+conn);
						conn = null;
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}, minutes * 1000);

			rs = pstmt.executeQuery();
			System.out.println("Time Taken using Execute Query :" +( System.currentTimeMillis() -t)); 
			timer.cancel();
			
		}catch (SQLException e)
			{
				System.out.println("SqlException");
				e.printStackTrace() ;
			}
			finally
			{
				System.out.println("Finally ");
				DAOConnectionManager.getInstance().closeConnections(rs,pstmt,conn );
			}

			System.out.println("End Start");
		}

	public static void main(String[] args) {
		System.out.println("Start");
		QueryTimer queryTimer = new QueryTimer(1);
		System.out.println("After Initializatop");
		queryTimer.start();
		System.out.println("End");
	}

	private String getQuery(){
		
		StringBuilder stringBuilder = new StringBuilder("");
//		stringBuilder.append("select *   from pres_user_sto_temp_2 p1 where p1.user_cod in ( ");
//		stringBuilder.append("select p2.user_cod from pres_user_sto_temp_2 p2  where p2.user_cod in ( ");
//		stringBuilder.append("select p3.user_cod from pres_user_sto_temp_2 p3  where p3.user_cod in ( ");
//		stringBuilder.append("select p4.user_cod from pres_user_sto_temp_2 p4  ) )  ) ");
		
//		stringBuilder.append("select *   from pres_user_sto_temp_2 p1 where p1.user_cod in ( ");
//		stringBuilder.append("select p2.user_cod from pres_user_sto_temp_2 p2  where p2.user_cod in ( ");
//		stringBuilder.append("select p3.user_cod from pres_user_sto_temp_2 p3  )  ) ");
		
		stringBuilder.append("select *   from pres_user_sto_temp_2 p1 where p1.user_cod in ( ");
		stringBuilder.append("select p2.user_cod from pres_user_sto_temp_2 p2  ) ");
		
//		StringBuilder stringBuilder = new StringBuilder("select * from  dbui_sc_lg_operation_tt t1 where t1.op_session_id in  ( ");
//		stringBuilder.append("select tt.op_session_id from  dbui_sc_lg_operation_tt tt where tt.op_session_id in  ( ");
//		stringBuilder.append(" select ttt.op_session_id from  dbui_sc_lg_operation_tt ttt  ) )");
		
		return stringBuilder.toString();
	}
	
}
